package cn.com.api.service.impl;

import java.util.List;
import java.util.Map;

import net.sf.ehcache.CacheManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.api.entity.ApiCar;
import cn.com.api.entity.ApiDingDan;
import cn.com.api.entity.ApiT_products;
import cn.com.api.mapper.ApiCarMapper;
import cn.com.api.mapper.ApiDingDanMapper;
import cn.com.api.mapper.ApiT_productsMapper;
import cn.com.api.resultpage.InitShopListPage;
import cn.com.api.service.ApiT_productsService;
import cn.com.api.service.ApiTypeService;
import cn.com.api.service.StartOnLoadService;
import cn.com.api.service.impl.base.T_productsServiceImplBase;
import cn.com.util.*;

@Service
public class ApiT_productsServiceImpl extends T_productsServiceImplBase
		implements ApiT_productsService {
	@Autowired
	private ApiTypeService apiTypeService;
	@Autowired
	private ApiT_productsMapper apiT_productsMapper;
	@Autowired
	private static CacheManager manager; 
	@Autowired
	private StartOnLoadService sartOnLoadService; 
	@Autowired
	private ApiCarMapper apiCarMapper;
	@Autowired
	private ApiDingDanMapper apiDingDanMapper;
	
	//
	@Override
	public List<Map<String, Object>> findProductsByGoodsId(Long goods_id) {
		List<Map<String, Object>> t_products = apiT_productsMapper
				.findProductsByGoodsId(goods_id);
		return t_products;
	}

	@Override
	public List<ApiT_products> getUndeletedProductsByGoodsId(Long goods_id) {
		List<ApiT_products> t_products = apiT_productsMapper
				.getProductsByGoodsId(goods_id,0);
		return t_products;
	}

	@Override
	public int insertT_productsReturnId(ApiT_products t_products) {
		//
		int ret_I = apiT_productsMapper.insertT_productsReturnId(t_products);
		return ret_I;
	}

	@Override
	public boolean updateTopOrDel(String del, String status, String id) {
		return apiT_productsMapper.updateTopOrDel(del, status, id);
	}
	
	@Override
	public void searchProductsByGoodName(Map<String, String> params,
			Map<String, Object> map) {
		String name = params.get("name");
		String city_id = params.get("city_id");
		String longitude = params.get("longitude");
		String latitude = params.get("latitude");
		Integer pageCount = Integer.valueOf(params.get("pageCount"));
		String str = sartOnLoadService.dictionaryMap().get("ACT_XINREN");
		
		PageInfo page = new PageInfo();
		page.setCurrentPage(pageCount);
		System.out.println("搜索的商品名称："+name+"city_id:"+city_id);
		List<ApiT_products> goodsList = apiT_productsMapper.searchProductsByGoodNameListPage(name,city_id,str,page);
		if(goodsList!=null && goodsList.size()>0){
			for(ApiT_products product : goodsList){
				Integer isEdit = product.getIsEdit();
				product.setIsEdit(apiTypeService.getShopType(isEdit));
			}
			
		}
		InitShopListPage initShopListPage = new InitShopListPage();
		initShopListPage.setProductsList(goodsList);
		initShopListPage.setPage(page);
		initShopListPage.setTotalPage(page.getTotalPage());
		map.put("datas", initShopListPage);
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "success");
	}
	
	@Transactional
	@Override
	public void modifyProductCountByCarId(Integer car_id) {
		//根据购物单号，查出   product_id   count
		ApiCar car = apiCarMapper.findApiCarById(car_id);
		if(car!=null){
			Long product_id = car.getProduct_id();
			Integer count = car.getCount();
			apiT_productsMapper.reduceProductByProductId(product_id, count);
		}
	}
	
	//取消的订单---根据订单号、查询购物车记录，恢复库存，已售数量
	@Transactional
	@Override
	public void modifyProductCountByOrderId(String orderId) {
		//根据订单号查询订单信息
		ApiDingDan dingDan = apiDingDanMapper.searchDingDanByTitleId(orderId);
		String carIds = dingDan.getCar_ids();
		if(carIds!=null && carIds.length()>0){
			String[] carArray = carIds.split(",");
			if(carArray!=null && carArray.length>0){
				for(String car_id : carArray){
					Integer carId = Integer.valueOf(car_id);
					System.out.println("恢复库存的购物车记录：carId"+carId);
					ApiCar car = apiCarMapper.findApiCarById(carId);
					if(car!=null){
						Long product_id = car.getProduct_id();
						Integer count = car.getCount();
						apiT_productsMapper.recoveryProductByProductId(product_id, count);
					}
				}
			}
		}
		
	}

	@Override
	public boolean updatePriceByPro(String price, String proid,String store) {
		return apiT_productsMapper.updatePriceByPro(price, proid,store);
	}

	@Override
	public String searStoreByProId(String proid) {
		return apiT_productsMapper.searStoreByProId(proid);
	}

	@Override
	public List<ApiT_products> findProductsByErpProductId(String erp_product_id) {
		// TODO
		List<ApiT_products> t_productss = apiT_productsMapper.findProductsByErpProductId( erp_product_id );
		return t_productss;
	}

	@Override
	public List<Map<String, Object>> searGoodIdAndCityId(String goods_id,
			String cityid) {
		return apiT_productsMapper.searGoodIdAndCityId(goods_id, cityid);
	}
	@Override
	public List<ApiT_products> searchProducts(Map<String, Object> conditions, PageInfo pageInfo) {
		return apiT_productsMapper.searchProductsListPage(conditions, pageInfo);
	}

	@Override
	public List<ApiT_products> findZiYingproductss() {
		
		return apiT_productsMapper.findZiYingproductss();
	}

	@Override
	public Map<String, Object> getGoodsInfoByProductId(String productId) {
		// TODO Auto-generated method stub
		return apiT_productsMapper.getGoodsInfoByProductId(productId);
	}
	
	@Override
	public String getProductNameProductId(String productId) {
		// TODO Auto-generated method stub
		return apiT_productsMapper.getProductNameProductId(productId);
	}

	@Override
	public void updateIsDeleteByProductId(String productId) {
		// TODO Auto-generated method stub
		apiT_productsMapper.updIsDeleteByProductId(productId);
		
	}
	
	
}
